package com.charge.web;

import com.charge.domain.Account;
import com.charge.domain.ColorModel;
import com.charge.domain.User;
import com.charge.service.AccountService;
import com.charge.service.ColorService;
import com.charge.util.Const;
import com.charge.util.WebResponse;
import com.sun.xml.internal.ws.resources.HttpserverMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Description : 账户管理
 * Author : 91658
 * Date : 2018/6/7 13:56
 **/
@Controller
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private ColorService colorService;

    @RequestMapping("/account_manage")
    public String accountManage(HttpServletRequest request, HttpSession session) {
        User user = (User) session.getAttribute("user");

        Account account = new Account();
        account.setUserId(user.getId());
        List<Account> accountList = accountService.selectAccountList(account);
        request.setAttribute("accountList", accountList);
        return "/account/account_manage";
    }

    @RequestMapping("/add_account_frame")
    public String addAccountFrame(HttpServletRequest request) {
        List<ColorModel> colorModelList = colorService.selectColorList();
        request.setAttribute("colorList", colorModelList);
        return "/account/add_account";
    }

    @RequestMapping("/add_account")
    @ResponseBody
    public WebResponse<String> addAccount(Account account, HttpSession session) {
        User user = (User) session.getAttribute("user");

        account.setUserId(user.getId());
        int row = accountService.insertAccount(account);
        if (row > 0) {
            return WebResponse.createBySuccess(Const.SUCCESS, "");
        }
        return WebResponse.createByFail(Const.FAIL, "");
    }

    @RequestMapping("/account_list")
    @ResponseBody
    public WebResponse<List<Account>> accountList(HttpSession session) {
        User user = (User) session.getAttribute("user");

        Account account = new Account();
        account.setUserId(user.getId());
        List<Account> accountList = accountService.selectAccountList(account);
        return WebResponse.createBySuccess(Const.SUCCESS, accountList);
    }

}
